<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>定时任务管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-table-cell .layui-btn-group .layui-btn {
            padding: 0 8px;
            height: 28px;
            line-height: 28px;
            font-size: 12px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">任务名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="jobName" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">任务状态</label>
                            <div class="layui-input-inline">
                                <select name="jobStatus" class="layui-input">
                                    <option value="">全部</option>
                                    <option value="0">已停用</option>
                                    <option value="1">已启动</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="button" class="layui-btn layui-btn-normal" id="data-reset-btn"><i class="layui-icon layui-icon-refresh"></i> 重 置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!-- 操作按钮模板 -->
        <script type="text/html" id="currentTableBar">
            <div class="layui-btn-group">
                {{# if(d.jobStatus === 1) { }}
                <button class="layui-btn layui-btn-danger layui-btn-xs toggle-btn" data-id="{{d.id}}" data-status="0">停止</button>
                {{# } else { }}
                <button class="layui-btn layui-btn-normal layui-btn-xs toggle-btn" data-id="{{d.id}}" data-status="1">启动</button>
                {{# } }}
                <button class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</button>
                <button class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</button>
            </div>
        </script>

        <!-- 添加页面 -->
        <div id="add" style="display: none">
            <div class="layui-form layuimini-form" lay-filter="addForm">
                <div class="layui-form-item">
                    <label class="layui-form-label required">任务名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="jobName" lay-verify="required" placeholder="请输入任务名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">Cron表达式</label>
                    <div class="layui-input-inline">
                        <input type="text" name="cronParam" lay-verify="required" placeholder="请输入Cron表达式" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">参数</label>
                    <div class="layui-input-inline">
                        <input type="text" name="jobParam" placeholder="请输入参数" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">任务描述</label>
                    <div class="layui-input-inline">
                        <input type="text" name="jobDesc" placeholder="请输入任务描述" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">任务状态 1-启动：0-停止</label>
                    <div class="layui-input-inline">
                        <input type="text" name="jobStatus" placeholder="请输入任务状态" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveAddBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 编辑页面 -->
        <div id="edit" style="display: none">
            <div class="layui-form layuimini-form" lay-filter="editForm">
                <div class="layui-form-item">
                    <label class="layui-form-label required">任务ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="id" readonly class="layui-input layui-disabled">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">任务名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="jobName" lay-verify="required" placeholder="请输入任务名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">Cron表达式</label>
                    <div class="layui-input-inline">
                        <input type="text" name="cronParam" lay-verify="required" placeholder="请输入Cron表达式" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">参数</label>
                    <div class="layui-input-inline">
                        <input type="text" name="jobParam" placeholder="请输入参数" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">任务描述</label>
                    <div class="layui-input-inline">
                        <input type="text" name="jobDesc" placeholder="请输入任务描述" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveEditBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'jquery'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        var index;

        // 初始化表格
        table.render({
            elem: '#currentTableId',
            url: '/sysjobtime/queryPage',
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list
                };
            },
            toolbar: '#toolbarDemo',
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 180, title: '任务ID', sort: true},
                {field: 'jobName', width: 200, title: '任务名称'},
                {field: 'cronParam', width: 200, title: 'Cron表达式'},
                {field: 'jobParam', width: 200, title: '任务参数'},
                {field: 'jobDesc', width: 200, title: '任务描述'},
                {
                    field: 'jobStatus',
                    width: 100,
                    title: '任务状态',
                    templet: function (d) {
                        return d.jobStatus === 1 ? '<span style="color:lime;">已启动</span>' : '<span style="color:red;">已停用</span>';
                    }
                },
                {title: '操作', minWidth: 240, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 搜索监听
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: { curr: 1 },
                where: data.field
            });
            return false;
        });

        // 重置按钮点击事件
        $('#data-reset-btn').on('click', function () {
            $('input[name="jobName"]').val('');
            $('select[name="jobStatus"]').val('');
            form.render('select'); // 强制重新渲染下拉框
            table.reload('currentTableId', {
                page: { curr: 1 },
                where: {}
            });
        });

        // 工具栏监听
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {
                form.val("addForm", {});
                index = layer.open({
                    title: '添加定时任务',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['75%', '75%'],
                    content: $('#add'),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        // 表格行操作监听
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                form.val("editForm", data);
                index = layer.open({
                    title: '编辑定时任务',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['75%', '75%'],
                    content: $('#edit'),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                $.getJSON('/sysjobtime/delete?id=' + data.id, function (res) {
                    if (res.code === 0) {
                        layer.msg("删除成功");
                        obj.del();
                    } else {
                        layer.msg("删除失败：" + res.msg || "未知错误");
                    }
                });
            }
        });

        // 启动/停止按钮点击事件
        $(document).on('click', '.toggle-btn', function () {
            var $btn = $(this);
            var id = $btn.data('id');
            var newStatus = $btn.data('status');

            // 显示加载动画
            var loadingIndex = layer.load(2);

            $.post('/sysjobtime/updateStatus', {
                id: id,
                jobStatus: newStatus
            }, function (res) {
                layer.close(loadingIndex);
                if (res.code === 0) {
                    layer.msg('状态更新成功');

                    // 更新当前行的状态显示和按钮样式
                    var tr = $btn.closest('tr');
                    var statusCell = tr.find('td[data-field="jobStatus"]');
                    if (newStatus == 1) {
                        statusCell.find('.layui-table-cell').html('<span style="color:lime;">已启动</span>');
                        $btn.removeClass('layui-btn-normal').addClass('layui-btn-danger').text('停止').data('status', 0);
                    } else {
                        statusCell.find('.layui-table-cell').html('<span style="color:red;">已停用</span>');
                        $btn.removeClass('layui-btn-danger').addClass('layui-btn-normal').text('启动').data('status', 1);
                    }
                } else {
                    layer.msg(res.msg || '状态更新失败');
                }
            }).fail(function () {
                layer.close(loadingIndex);
                layer.msg('网络请求失败，请检查');
            });
        });


        // 添加提交
        form.on('submit(saveAddBtn)', function (data) {
            $.post("/sysjobtime/add", data.field, function (res) {
                if (res.code === 0) {
                    layer.msg("添加成功");
                    table.reload('currentTableId');
                } else {
                    layer.msg("添加失败");
                }
                layer.close(index);
            }, "json");
            return false;
        });

        // 编辑提交
        form.on('submit(saveEditBtn)', function (data) {
            $.post("/sysjobtime/update", data.field, function (res) {
                if (res.code === 0) {
                    layer.msg("修改成功");
                    table.reload('currentTableId');
                } else {
                    layer.msg("修改失败");
                }
                layer.close(index);
            }, "json");
            return false;
        });

    });
</script>

</body>
</html>
